package jdbc.图书管理系统;


import jdbc.mobel.jdlist;
import jdbc.util.BaseDao;

import java.util.List;
import java.util.Scanner;
public class library {
    static String zhanghao="lh189";//密码
    static String mi="12306";//账号

    static String e1="水浒传";
    static String e2="计算机基础";
    static String e3="数据库原理";
    static String e4="高等数学";
    static String e5="离散数学";
    static int a1=50;
    static int a2=31;
    static int a3=35;
    static int a4=42;
    static int a5=51;

   //--------------------------------------------------------------------------------------TODO 登录
        public static void setDeng(){
            Scanner dr=new Scanner(System.in);
            System.out.println("-------------------------------");
            System.out.println("请输入账号");
            String zh=dr.next();
            System.out.println("请输入密码");
            String ma=dr.next();
            System.out.println("-------------------------------");
            if (zh.equals(zhanghao) && ma.equals(mi)) {
                System.out.println("登陆成功!!!");
                System.out.println("欢迎进入图书管理系统");
                System.out.println("请选择借书or还书or查询");
                System.out.println("1.借书");
                System.out.println("2.还书");
                System.out.println("3.查询书籍目录");
                System.out.println("4.退出");
                System.out.println("--------------------------------");
                int a=dr.nextInt();
                if (a==1) {
                    System.out.println("好的为您跳转到借书页面");
                    libraryBorrow();//借书
                } else if (a==2) {
                    System.out.println("好的为您跳转到还书页面");
                    libraryReturn();//还书
                }else if (a==3) {
                    let();
                    System.out.println("登录之后才能借书或者还书哦");
                    setDeng();
                }else if (a==4) {
                    System.exit(0);
                }else {//处理不按照格式输入的
                    System.out.println("输入格式错误,请重新输入");
                }
            }else{
                System.out.println("账号或密码错误");
                setDeng();
            }
        }
        //-------------------------------------------------------------------------------------TODO 借书
        public static void libraryBorrow(){
            Scanner dr=new Scanner(System.in);
            let();
            System.out.println("请输入序号选择您想借的书");

            int a=dr.nextInt();
            int w1=2300+a;
            String j1="delete from book where id=?;";
            String j2="insert into book(id,name,math)values (?,?,?);";
            String sel="select * from book where id=?;";
            List<jdlist> se= BaseDao.executeDQL(sel, jdlist.class,w1);//接收

            int dat= se.size();//获取目前总数
            if (dat >0) {
                BaseDao.executeDML(j1,w1);//选择删除目标
                for (int i = 0; i <dat-1 ; i++) {//再添加总数减一
                   switch (a){
                       case 1:BaseDao.executeDML(j2,w1,e1,a1);break;
                       case 2:BaseDao.executeDML(j2,w1,e2,a2);break;
                       case 3:BaseDao.executeDML(j2,w1,e3,a3);break;
                       case 4:BaseDao.executeDML(j2,w1,e4,a4);break;
                       case 5:BaseDao.executeDML(j2,w1,e5,a5);break;
                       default : System.out.println("请输入正确号码");break;
                   }
                }
            }else{
                System.out.println("此书已借光");
            }
            System.out.print("本书剩余数量:  ");
            System.out.println(se.size()-1);
            System.out.println("借书成功,将为您返回登录页面");
            System.out.println("感谢您的支持,祝您学业有成");
            System.out.println("-----------------------------------------");
            setDeng();
        }
    public static void let(){
        System.out.println("--------------------------------------");
        String sel="select * from book where id=?;";
        int w1=2301;
        int w2=2302;
        int w3=2303;
        int w4=2304;
        int w5=2305;
        List<jdlist> se1= BaseDao.executeDQL(sel, jdlist.class,w1);//接收
        List<jdlist> se2= BaseDao.executeDQL(sel, jdlist.class,w2);//接收
        List<jdlist> se3= BaseDao.executeDQL(sel, jdlist.class,w3);//接收
        List<jdlist> se4= BaseDao.executeDQL(sel, jdlist.class,w4);//接收
        List<jdlist> se5= BaseDao.executeDQL(sel, jdlist.class,w5);//接收
        System.out.println("--------------------------------------");
        System.out.println("现在有以下图书和剩余数量");
        System.out.println("书名      剩余数量");
        System.out.println("1.水浒传: "+se1.size());
        System.out.println("2.计算机基础: "+se2.size());
        System.out.println("3.数据库原理: "+se3.size());
        System.out.println("4.高等数学: "+se4.size());
        System.out.println("5.离散数学: "+se5.size());
    }
        //----------------------------------------------------------------------------------- TODO 还书
    public static void libraryReturn(){
        Scanner dr=new Scanner(System.in);
        String j2="insert into book(id,name,math)values (?,?,?);";
        String sel="select * from book where id=?;";
        let();
        System.out.println("请输入要还的书");
        int a=dr.nextInt();
        int w1=2300+a;
        List<jdlist> se= BaseDao.executeDQL(sel, jdlist.class,w1);//接收
            switch (a){
                case 1:BaseDao.executeDML(j2,w1,e1,a1);break;
                case 2:BaseDao.executeDML(j2,w1,e2,a2);break;
                case 3:BaseDao.executeDML(j2,w1,e3,a3);break;
                case 4:BaseDao.executeDML(j2,w1,e4,a4);break;
                case 5:BaseDao.executeDML(j2,w1,e5,a5);break;
                default : System.out.println("请输入正确号码");break;
            }
        System.out.print("本书剩余数量:  ");
        System.out.println(se.size()+1);
        System.out.println("还书成功,将为您返回登录页面");
        System.out.println("感谢您按时归还,祝您幸福常伴");
        System.out.println("-----------------------------------------");
        setDeng();
    }

    public static void main(String[] args) {
        library a=  new library();
        a.setDeng();
    }

    }

